/**
 * Copyright 2012 ibiblio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ibiblio.terasaur.messaging.webqueue
import org.apache.commons.logging.LogFactory

abstract class Command {
    private static final log = LogFactory.getLog(this)
    protected Map _params

    protected Command(Map params) {
        _params = params
    }

    /**
     * Factory method for creating command objects.  The action value determines the
     * command class.
     *
     * @param Map params
     * @return Command
     */
    public static Command create(Map params) {
        Command c = null
        if (!params['action']) {
            throw new Exception('Missing action in message')
        }

        if ("add_torrent".equals(params.action)) {
            c = new AddTorrentCommand(params)
        } else {
            throw new Exception("Invalid action in message (" + params.action + ")")
        }

        return c
    }

    public abstract Map run()
}
